<?php

use Biz\module\Classes\wiki\Infos as clsInfos;
use Biz\pockets\constInterface\WikiFields;
use Biz\service\TodayNiceGoods\TodayNIceGoodsService;


/**
 * Description: 往期新品
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/6/16
 * Time: 10:22
 */
class BeforeListAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_NOTHING;
    public const IS_LOG_EXPOSE = 1;

    public function main()
    {
        $uid = $this->uid;
        //查询符合展示时间的wiki
        $page = $this->getRequest()->getPost('page', 1);
        $per_page = $this->getRequest()->getPost('per_page', 31);
        $month = $this->getRequest()->getPost('month', date("Y-m"));
        //初始化
        $data = [
            'total' => 0,
            'is_over' => 1,
            'current_page' => $page,
            'list' => [],
        ];
        //获取 想要的字段
        $field = [
            'brand_id',
//            'brand_name',
            'basic_id',
            'wiki_id',
            'img',
            "back_img",
            "back_show_img",
            'title',
            'sec_title',
            'tags',
            'sort',
            'end_at',
            'month'
        ];
        //偏移量，当前页-1乘以每页显示条数
        $start = ($page - 1) * $per_page;
        //获取活动商品列表
        $list = TodayNIceGoodsService::getBeforeList($field, $start, $per_page, $month) ?? [];
        $list = TodayNIceGoodsService::formatList($list);
        if (!$list) {
            return $this->setSuccess($data);
        }
        $wikiIds = array_column($list, 'wiki_id');
        $wikiLists = clsInfos::infos($wikiIds, WikiFields::FIELDS_GROUP_TNG_BEFORE_LIST, $uid);
        foreach ($list as &$item) {
            $item['cover_img'] = $wikiLists[$item['wiki_id']]['cover_img'] ?: '';
            $item['brand_info'] = $wikiLists[$item['wiki_id']]['brand_info'] ?: null;
            $item['want_person_avatars'] = $wikiLists[$item['wiki_id']]['want_person_avatars'] ?: [];
            $item['want_person'] = $wikiLists[$item['wiki_id']]['want_person'] ?: '';
            $item['h5_open_other_link'] = $wikiLists[$item['wiki_id']]['open_other_link'] ?: [];
        }
        $current = $page * $per_page;
        $data['total'] = TodayNIceGoodsService::getBeforeListCount($month);
        $data['is_over'] = ($data['total'] <= $current) ? 1 : 0;
        $data['list'] = $list;
        return $this->setSuccess($data);
    }
}